package com.john.springBootWebSocket;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class controller {
	
	@Autowired
	private SimpMessagingTemplate messagingTemplate;
	
	@MessageMapping("/chat")
	public void handleChat(Principal principal, String msg) {
		if(principal.getName().equals("john")){
			messagingTemplate.convertAndSendToUser("yanzhengwei", "/queue/notifications", principal.getName()+"-send:"+msg);
		}else{
			messagingTemplate.convertAndSendToUser("john", "/queue/notifications", principal.getName()+"-send:"+msg);
		}
	}
	
	
	@MessageMapping("/welcome")
	@SendTo("/topic/getResponse")
	public Response say(Message msg) {
		return new Response("Welcome, "+msg.getName()+"!");
	}
	
	

}
